package cn.edu.jxust.controller.api.area;

import cn.edu.jxust.common.domain.entity.Area;
import cn.edu.jxust.common.domain.dto.AreaVo;
import cn.edu.jxust.common.domain.web.api.area.IAreaController;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import cn.edu.jxust.core.domain.repository.AreaService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiuKangming
 * @since 2020-06-29
 *
 */

@Slf4j
@RestController
public class AreaController implements IAreaController {

    @Resource
    private AreaService areaService;

    @Override
    public Result addArea(AreaVo areaVo, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return areaService.save(
                    Area.builder()
                            .areaName(areaVo.getAreaName())
                            .editor(areaVo.getEditor())
                            .build())
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "区域信息增加成功")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "区域信息增加失败");
        }catch (Exception e){
            log.error("[AreaController --> addArea]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result delArea(Integer id){

        try{
            return areaService.delAreaById(id);
        }catch (Exception e){
            log.error("[AreaController --> delArea]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result delAreaBatch(String areaIdsStr){

        try {
            return areaService.delAreaBatch(areaIdsStr);
        }catch (Exception e){
            log.error("[AreaController --> delAreaBatch 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result modifyArea(AreaVo areaVo, BindingResult result){

        if(result.hasErrors() || null == areaVo.getId()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return areaService.updateById(
                    Area.builder()
                            .id(areaVo.getId())
                            .areaName(areaVo.getAreaName())
                            .editor(areaVo.getEditor())
                            .build())
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "区域信息修改成功")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "区域信息修改失败, 请重试!");
        }catch (Exception e){
            log.error("[AreaController --> modifyArea 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result findAreaPage(Integer page, Integer size){

        try{
            IPage<Area> pageInfo = areaService.page(new Page<>(page, size));
            if(pageInfo.getRecords() != null && !pageInfo.getRecords().isEmpty()){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, pageInfo);
            }else{
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何区域信息");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("[AreaController --> findAreaPage 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result findAreaList(){

        try{
            List<Area> list = areaService.list();
            if(null != list && list.size() != 0){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, list);
            }else{
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何有关区域信息");
            }
        }catch (Exception e){
            log.error("[AreaController --> findAreaList 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }
}

